/**
 * 
 */
package it.gommonauti.patenti.entity;

import java.util.ArrayList;

/**
 * @author Francesco Stallone ( f.stallone73@gmail.com ).
 * 
 * The exam.
 *
 */
public class Exam {
	/**
	 * Categories
	 */
	ArrayList<Category> categories;
	
	/**
	 * Max time in seconds.
	 */
	private int timeInSeconds;
	
	/**
	 * Max errors.
	 */
	private int maxErrors;

	/**
	 * Exam id.
	 */
	private int id;
	
	/**
	 * Exam description.
	 */
	private String description;
	
	/**
	 * Enabled or disable exam. 
	 */
	private boolean enabled;
	
	/**
	 * Exam description language.
	 */
	private Language language;
	
	/**
	 * @return the language
	 */
	public Language getLanguage() {
		return language;
	}

	/**
	 * @param language the language to set
	 */
	public void setLanguage(Language language) {
		this.language = language;
	}
	
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}
	
	/**
	 * @return the timeInSeconds
	 */
	public int getTimeInSeconds() {
		return timeInSeconds;
	}

	/**
	 * @param timeInSeconds the timeInSeconds to set
	 */
	public void setTimeInSeconds(int timeInSeconds) {
		this.timeInSeconds = timeInSeconds;
	}

	/**
	 * @return the maxErrors
	 */
	public int getMaxErrors() {
		return maxErrors;
	}

	/**
	 * @param maxErrors the maxErrors to set
	 */
	public void setMaxErrors(int maxErrors) {
		this.maxErrors = maxErrors;
	}
	
	/**
	 * @return the enabled
	 */
	public boolean isEnabled() {
		return enabled;
	}

	/**
	 * @param enabled the enabled to set
	 */
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	
	/**
	 * @return the categories
	 */
	public ArrayList<Category> getCategories() {
		return categories;
	}

	/**
	 * @param categories the categories to set
	 */
	public void setCategories(ArrayList<Category> categories) {
		this.categories = categories;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		
		StringBuffer sb = new StringBuffer();
		
		sb.append("ID: " + this.id + System.getProperty("line.separator"));
		
		sb.append("DESCRIPTION: " + this.description + System.getProperty("line.separator"));
		
		sb.append("Enabled: " + this.enabled + System.getProperty("line.separator"));
		
		sb.append("timeInSeconds: " + this.timeInSeconds + System.getProperty("line.separator"));
		
		sb.append("maxErrors: " + this.maxErrors + System.getProperty("line.separator"));
		
		return sb.toString();
	}
	
	/**
	 * Estabilish exam success. 
	 */
	public boolean isSuccess(){
		
		if (categories == null) return false;
		
		for (Category category : categories) {
			if (!category.isSuccess()) return false;
		}
		
		return getNumUserFails() + getNumUserNoAnswer() < maxErrors;
		
	}
	
	/**
	 * Error count
	 */
	public int getNumUserFails(){
		
		int count = 0;
		if (categories == null) return count;
		
		for (Category category : categories) {
			count += category.getNumUserFails();
		}
		
		return count;
	}
	
	/**
	 * Category count
	 */
	public int getCategotiesCount(){
		if (categories == null) return 0;
		return categories.size();
	}
	
	/**
	 * Main Question count
	 */
	public int getMainQuestionsCount(){
		int count = 0;
		
		if (categories == null) return 0;
		
		for (Category category : categories) {
			count += category.getQuestionInExam();
		}
		
		return count;
	}
	
	/**
	 * Main Question count
	 */
	public int getSubQuestionsCount(){
		int count = 0;
		
		if (categories == null) return 0;
		
		for (Category category : categories) {
			count += category.getSubQuestionsCount();
		}
		
		return count;
	}
	
	/**
	 * Get no answer question count
	 */
	public int getNumUserNoAnswer(){
		int count = 0;
		
		if (categories == null) return 0;		
		for (Category category : categories) {
			count += category.getNumUserNoAnswer();
		}
		
		return count;
	}
	
	/**
	 * Get success user count
	 */
	public int getNumUserSuccess(){
		int count = 0;
		
		if (categories == null) return 0;		
		for (Category category : categories) {
			count += category.getNumUserSuccess();
		}
		
		return count;
	}
}
